#include "CmdUtil.h"
#include "Widget.h"

#include <QApplication>
#include <QSettings>
#include <QString>
#include <QDebug>
#include <QCommandLineParser>

void addRightClickMenu() {
    // 注册表路径
    QString basePath = "HKEY_CLASSES_ROOT\\*\\shell\\SendToMyApp";

    // 创建QSettings对象
    QSettings settings(basePath, QSettings::NativeFormat);

    // 设置右键菜单的显示名称
    settings.setValue(".", "Send to My App");

    // 设置命令路径
    QString commandPath = basePath + "\\command";
    QSettings commandSettings(commandPath, QSettings::NativeFormat);
    commandSettings.setValue(".", "\"D:\\Qt\\CLionProjects\\qt-project\\ZdRun\\build\\Desktop_Qt_5_15_2_MinGW_64_bit-Debug\\debug\\ZdRun.exe\" \"%1\"");

    // 同步注册表
    settings.sync();
    commandSettings.sync();

    // 检查是否成功
    if (settings.status() == QSettings::NoError && commandSettings.status() == QSettings::NoError) {
        qDebug() << "右键菜单添加成功";
    } else {
        qDebug() << "右键菜单添加失败";
    }
}
int main(int argc, char *argv[])
{
    CmdUtil::checkAndKillProcess();
    QApplication a(argc, argv);
    Widget w;
    w.setWindowTitle("ZdRun");
    // 调用函数添加右键菜单
    // addRightClickMenu();

    QCommandLineParser parser;
    parser.addPositionalArgument("file", "The file to be processed.");
    parser.process(a);

    QString sendToStr = CmdUtil::getSendToDirectory();
    qDebug() << "sendToStr=" << sendToStr;
    const QStringList args = parser.positionalArguments();
    if (!args.isEmpty()) {
        w.handleFile(args.first());
    }else{
        w.show();
    }
    return a.exec();
}
